##=============================================================================
## Main Manuscript: Table 4
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "kableExtra", "broom", "hrbrthemes",
              "modelsummary", "infer")

ipak(packages)

##---------
# Load data
#setwd("~/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##---------------------
# Russian Speakers Only
# dat %>% 
#   filter(home_lang == 2) %>%
#   group_by(wave) %>% 
#   count(home_lang)
# 
# dat %>%  
#   filter(wave == 0 & home_lang == 2) %>% # n = 475
#   reframe(quantile(survival_mindset_z, na.rm = TRUE)) # 0.710 
# 
# dat %>%  
#   filter(wave == 1 & home_lang == 2) %>% # n = 376
#   reframe(quantile(survival_mindset_z, na.rm = TRUE)) # 0.710

# Wave I
rus_speak_1 <- dat %>% 
  filter(wave == 0 & home_lang == 2) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.710, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>% 
  dplyr::select(id, compromise_idx_full_z, save_lives_z, survival_mindset_z,
                survival_binary)
# Wave II
rus_speak_2 <- dat %>% 
  filter(wave == 1 & home_lang == 2) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.710, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>% 
  dplyr::select(id, compromise_idx_full_z, save_lives_z, survival_mindset_z,
                survival_binary)

# Wave I -- Save Lives (z-std)
rus_speak_1 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = save_lives_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave I -- Compromise (z-std)
rus_speak_1 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = compromise_idx_full_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave II -- Save Lives (z-std)
rus_speak_2 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = save_lives_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave II -- Compromise (z-std)
rus_speak_2 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = compromise_idx_full_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

rm(rus_speak_1, rus_speak_2)

##---------------------
# Bilingual (UKR + RUS)
#dat %>% 
#  filter(home_lang == 1) %>%
#  group_by(wave) %>% 
#  count(home_lang) # 1066 and 1201

# dat %>%  # n = 1,214
#   filter(wave == 0 & home_lang == 2 | home_lang == 3 | home_lang == 7) %>% 
#   reframe(quantile(survival_mindset_z, na.rm = TRUE)) # 0.710

#dat %>%  # n = 1,115
#  filter(wave == 1 & home_lang == 2 | home_lang == 3 | home_lang == 7) %>% 
#  reframe(quantile(survival_mindset_z, na.rm = TRUE)) # 0.710 

# Wave I
rus_bilingual_1 <- dat %>% 
  filter(wave == 0 & home_lang == 2 | home_lang == 3 | home_lang == 7) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.710, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>% 
  dplyr::select(id, compromise_idx_full_z, save_lives_z, survival_mindset_z,
                survival_binary)

# Wave II
rus_bilingual_2 <- dat %>% 
  filter(wave == 1 & home_lang == 2 | home_lang == 3 | home_lang == 7) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.710, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>% 
  dplyr::select(id, compromise_idx_full_z, save_lives_z, survival_mindset_z,
                survival_binary)

# Wave I -- Save Lives (z-std)
rus_bilingual_1 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = save_lives_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave I -- Compromise (z-std)
rus_bilingual_1 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = compromise_idx_full_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave II -- Save Lives (z-std)
rus_bilingual_2 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = save_lives_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave II -- Compromise (z-std)
rus_bilingual_2 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = compromise_idx_full_z  ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

rm(rus_bilingual_1, rus_bilingual_2)

##--------------------------
# Ethnic Russian Respondents
#dat %>% 
#  filter(ethnic == 2) %>% 
#  group_by(wave) %>% 
#  count(ethnic) # 49, 49

#dat %>%  
#  filter(wave == 0 & ethnic == 2) %>% # n = 49
#  reframe(quant75 = quantile(survival_mindset_z, na.rm = TRUE)) # 1.11

#dat %>%  
#  filter(wave == 1 & ethnic == 2) %>% # n = 49
#  reframe(quant75 = quantile(survival_mindset_z, na.rm = TRUE)) # 0.710

rus_eth_1 <- dat %>% 
  filter(wave == 0 & ethnic == 2) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 1.11, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>% 
  dplyr::select(id, compromise_idx_full_z, save_lives_z, survival_mindset_z,
                survival_binary) 

rus_eth_2 <- dat %>% 
  filter(wave == 1 & ethnic == 2) %>% 
  mutate(survival_binary = ifelse(survival_mindset_z > 0.710, 1, 0)) %>% 
  mutate(survival_binary = factor(survival_binary, 
                                  labels = c("Low", "High"))) %>% 
  dplyr::select(id, compromise_idx_full_z, save_lives_z, survival_mindset_z,
                survival_binary)

# Wave I -- Save Lives (z-std)
rus_eth_1 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = save_lives_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave I -- Compromise (z-std)
rus_eth_1 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = compromise_idx_full_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave II -- Save Lives (z-std)
rus_eth_2 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = save_lives_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

# Wave II -- Compromise (z-std)
rus_eth_2 %>% 
  tidyr::drop_na(survival_binary) %>%
  t_test(formula = compromise_idx_full_z ~ survival_binary, 
         alternative = "two-sided", order = c("Low", "High"))

rm(rus_eth_1, rus_eth_2)

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================